package com.ruoyi.customer.service.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.customer.domain.CsCustomer;
import com.ruoyi.customer.service.ICsSeaService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.customer.mapper.CsSeaMapper;
import com.ruoyi.customer.domain.CsSea;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.Validator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanValidators;

/**
 * 客户公海Service业务层处理
 * 
 * @author 李守壮
 * @date 2023-03-16
 */
@Service
public class CsSeaServiceImpl implements ICsSeaService
{

    private static final Logger log = LoggerFactory.getLogger(CsSeaServiceImpl.class);

    @Autowired
    protected Validator validator;

    @Autowired
    private CsSeaMapper csSeaMapper;

    /**
     * 查询客户公海
     * 
     * @param id 客户公海主键
     * @return 客户公海
     */
    @Override
    public CsSea selectCsSeaById(Long id)
    {
        return csSeaMapper.selectCsSeaById(id);
    }

    /**
     * 查询客户公海列表
     * 
     * @param csSea 客户公海
     * @return 客户公海
     */
    @Override
    public List<CsSea> selectCsSeaList(CsSea csSea)
    {
        return csSeaMapper.selectCsSeaList(csSea);
    }

    /**
     * 新增客户公海
     * 
     * @param csSea 客户公海
     * @return 结果
     */
    @Override
    public int insertCsSea(CsSea csSea)
    {
        String contractCode = "ZJCS"+DateUtils.getTimestampNow();
        csSea.setCompanyCode(contractCode);
        csSea.setCreateTime(DateUtils.getNowDate());
        return csSeaMapper.insertCsSea(csSea);
    }

    /**
     * 修改客户公海
     * 
     * @param csSea 客户公海
     * @return 结果
     */
    @Override
    public int updateCsSea(CsSea csSea)
    {
        csSea.setUpdateTime(DateUtils.getNowDate());
        csSea.setUpdateBy(SecurityUtils.getLoginUserIdTypeString());
        return csSeaMapper.updateCsSea(csSea);
    }

    /**
     * 批量删除客户公海
     * 
     * @param ids 需要删除的客户公海主键
     * @return 结果
     */
    @Override
    public int deleteCsSeaByIds(Long[] ids)
    {
        for(Long id: ids){
            CsSea csSea = new CsSea();
            csSea.setId(id);
            csSea.setDelFlag("2");
            csSea.setUpdateTime(DateUtils.getNowDate());
            csSea.setUpdateBy(SecurityUtils.getLoginUserIdTypeString());
            csSeaMapper.updateCsSea(csSea);
        }
        return 1;
    }

    /**
     * 删除客户公海信息
     * 
     * @param id 客户公海主键
     * @return 结果
     */
    @Override
    public int deleteCsSeaById(Long id)
    {
        CsSea csSea = new CsSea();
        csSea.setId(id);
        csSea.setDelFlag("2");
//        csSea.setUpdateTime(DateUtils.getNowDate());
        csSea.setUpdateBy(SecurityUtils.getLoginUserIdTypeString());
        return csSeaMapper.updateCsSea(csSea);
    }






    /**
     * 导入数据
     */
    @Override
    public String importData(List<CsSea> list, Boolean isUpdateSupport, String operName)
    {
        if (ObjectUtils.isEmpty(list))
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CsSea record : list)
        {
            try
            {
                // 验证是否存在这个数据
                String companyName =record.getCompanyName();
                if (companyName == null || companyName.equals("")){
                    String msg = "<br/> 没有客户名称会报错的， 导入失败：";
                    failureMsg.append(msg);
                }
                CsSea u= getByCompanyName(companyName);
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, record);
                    this.insertCsSea(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户名称： " + record.getCompanyName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, record);
                    record.setUpdateBy(operName);
                    this.updateCsSea(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户名称： " + record.getCompanyName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、客户名称： " + record.getCompanyName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、客户名称： " + record.getCompanyName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



    /**
     * 用客户名称找，因为中国公司全称不可以雷同
     */
    public CsSea getByCompanyName(String companyName) {
        CsSea obj = new CsSea();
        obj.setCompanyName(companyName);
        List<CsSea> results = csSeaMapper.selectCsSeaList(obj);
        if (ObjectUtils.isNotEmpty(results)) {
            return results.get(0);
        }
        return null;
    }




}
